// 2025/11/21
// 在排序数组中查找元素的第一个和最后一个位置

class Solution {
public:
    vector<int> searchRange(vector<int>& nums, int target) {
        int l = 0, r = nums.size() - 1;
        vector<int> ans = {-1, -1};
        while(l <= r)
        {
            int mid = (l + r) >> 1;
            if(nums[mid] >= target)
            {
                if(nums[mid] == target)
                    ans[0] = mid;
                r = mid - 1;
            }
            else
                l = mid + 1;
        }
        l = 0, r = nums.size() - 1;
        while(l <= r)
        {
            int mid = (l + r) >> 1;
            if(nums[mid] <= target)
            {
                if(nums[mid] == target)
                    ans[1] = mid;
                l = mid + 1;
            }
            else
                r = mid - 1;
        }
        return ans;
    }
};